package com.anysoft.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.bean.QueryFundListBean;

import java.util.List;

/**
 * Created by wwq on 2017/3/1.
 */

public class FundAdapter extends BaseAdapter {

    private Context context;
    private List<QueryFundListBean.DataBean> itemList;
    private LayoutInflater inflater;

    private OnBuyQuicklyBtnListener onBuyQuicklyBtnListener;

    public void setOnBuyQuicklyBtnListener(OnBuyQuicklyBtnListener onBuyQuicklyBtnListener) {
        this.onBuyQuicklyBtnListener = onBuyQuicklyBtnListener;
    }

    public FundAdapter(Context context, List<QueryFundListBean.DataBean> itemList) {
        this.itemList = itemList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        FundSelectedViewHolder holdler;

        if (convertView == null) {
            holdler = new FundSelectedViewHolder();
            convertView = inflater.inflate(R.layout.item_fund_selected, null);

            holdler.btn_item_purchase_fund = (Button) convertView
                    .findViewById(R.id.btn_item_purchase_fund);
            holdler.tv_item_purchase_fund_name = (TextView) convertView
                    .findViewById(R.id.tv_item_purchase_fund_name);
            holdler.tv_item_purchase_fund_introduction = (TextView) convertView
                    .findViewById(R.id.tv_item_purchase_fund_introduction);
            holdler.tv_item_purchase_fund_net_value = (TextView) convertView
                    .findViewById(R.id.tv_item_purchase_fund_net_value);
            holdler.tv_item_purchase_fund_rate_of_return = (TextView) convertView
                    .findViewById(R.id.tv_item_purchase_fund_rate_of_return);

            convertView.setTag(holdler);
        } else {
            holdler = (FundSelectedViewHolder) convertView.getTag();
            resetViewHolder(holdler);
        }

        final QueryFundListBean.DataBean bean = itemList.get(position);

        holdler.tv_item_purchase_fund_name.setText(bean.getFundname());
        holdler.tv_item_purchase_fund_introduction.setText(bean.getFundIntroduceStr());
        holdler.tv_item_purchase_fund_net_value.setText(bean.getPernetvalue());
        holdler.tv_item_purchase_fund_rate_of_return.setText(bean.getPeriodSinceEstablish());

        holdler.btn_item_purchase_fund.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onBuyQuicklyBtnListener != null) {
                    onBuyQuicklyBtnListener.OnBuyQuicklyBtnClick(bean);
                }
            }
        });

        return convertView;
    }

    private void resetViewHolder(FundSelectedViewHolder holder) {
//        holder.btn_item_purchase_fund.setText("默认值");
    }

    public interface OnBuyQuicklyBtnListener {

        /**
         * @param bean
         */
        void OnBuyQuicklyBtnClick(QueryFundListBean.DataBean bean);
    }
}
